﻿package com.powerunion.extjs.utils;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class ComboboxUtils extends DaoFactoryUtils{
	public HttpServletRequest request = null;
	public Collection rs = null;

	public ComboboxUtils(){
		this.sql = null;
	}
	
	/**
	 * 重写父类list方法,实现通用查询下拉数据框
	 */
    public String list(){
    	Object o = Instance.getTreeHql(this.getRequest("hqlTag"));
    	String flag = "hql";
    	if(o.getClass() == TreeParaVo.class){
    		TreeParaVo vo = (TreeParaVo)o;
    		flag = vo.getFlag();
    		this.sql = vo.getSql();
    	}else{
    		this.sql = (String)o;
    	}
    	
//    	this.sql = Instance.getQueryHql(this.getRequest("hqlTag"));
    	if(this.sql == null) return "";
    	String str = this.getRequest("params");

    	String params[] = str.split(",");
    	boolean f = true; 
    	for(int i=0; i<params.length; i++){
    		if(params[i].length() > 0){
    			f = false;
    			break;
    		}
    	}
    	if(f) params = null;

    	this.dao.currentSession();
    	boolean sqlflag = "sql".equals(flag);
    	Query query = this.dao.getQuery(this.sql, params, sqlflag);
    	Collection col = query.list();
    	String[] colName = null;
    	if (!sqlflag){
    		colName = query.getReturnAliases();
    	}else{
    		colName = new String[2];
    	}
    	
    	this.dao.closeSession();
    	
    	String ren = this.dao.toComboboxMutiValue(col,colName);
    	
        return ren;
    }

    
	/**
	 * 实现通用tree
	 */
    public String tree(){
    	String flag ="hql";//标志:按照sql还是hql来解析查询语句
    	String treesql= "";//树的生成语句
    	String searchsql="";//树的查询语句
    	Object o = Instance.getTreeHql(this.getRequest("hqlTag"));
    	if(o.getClass() == TreeParaVo.class){
    		TreeParaVo vo = (TreeParaVo)o;
    		flag = vo.getFlag();
    		treesql = vo.getSql();
    		searchsql = vo.getSearch_sql();
    	}else{
    		treesql = (String)o;
    	}
    	if(treesql == null || "".equals(treesql)) return "";
    	
    	this.sql = treesql;
    	
    	String baseParams = this.getRequest("baseParams");
    	String p = this.getRequest("node");
    	//是否是快速检索,快速检索时传的参数格式为  "search":value:参数的个数,如果baseParams本身还有参数,放在后面用:分割．
    	boolean isSearchParam = (baseParams.indexOf(":")>=0); 
    	
    	if((baseParams.length() > 0)&& !isSearchParam){
    		p = p + "," + baseParams;
    	}
    	String params[] = p.split(",");//{this.getRequest("node")};
    	if(params[0] == "") params[0] = "0";//上级[parentid]

    	if(params[0].indexOf("|") >= 0){//如果node[0]中存在其他信息，则取第一个为parentid
    		params[0] = params[0].split("\\|")[0];
    	}
    	
    	//如果是快速检索,并且parentid为0时,替换sql语句,设置查询参数
    	if((isSearchParam) &&("0".equals(params[0]))){
    		String [] base = baseParams.split(":");
    		int num = Integer.parseInt(base[2]); //快速检索参数个数
    		int total = num + base.length-3 ;//base.length-3 为其它参数个数
    		this.sql = searchsql;
    		params = new String[total]; 
    		for(int i=0;i<total;i++){
    			if(i<num){
    				params[i] = base[1];
    			}else{
    				params[i]=base[3+i-num];
    			}
    		}	
    	}
    	
    	this.dao.currentSession();
    	
    	if("sql".equals(flag)){
    		rs = this.dao.getResultsWithSql(this.sql, params, false);	
    	}else{
    		rs = this.dao.getResults(this.sql, params, false);	
    	}
    	
    	this.dao.closeSession();
    	
    	String str = this.dao.toTree(rs);
    	String cls = this.getRequest("cls");
    	if(cls != "") str = str.replaceAll("'cls' :'folder'", "'cls' :'" + cls + "'");
    	return str;
    }
    
	/**
	 * 实现地区级联下拉框
	 */
    public String region(){
    	String hqlTag = this.getRequest("hqlTag");
    	String parent = this.getRequest("parent");
    	if(parent == null || parent.equals("")) return "";
    	this.sql = Instance.getQueryHql(hqlTag);
    	if(this.sql == null) return "";
    	String params[] = new String[1];
    	
    	if(parent.indexOf("#") < 0){
    		params = null;
    	}
    	else{
    		params[0] = parent.split("#")[1];
    	}

    	this.dao.currentSession();
    	rs = this.dao.getResults(this.sql, params, false);
    	this.dao.closeSession();
    	
        return this.dao.toCombobox(rs);
    }

    /**
     * 从request中获得参数值
	 * @return string
	 */
	public String getRequest(String parameter) {
		if(this.request == null) return "";
		return this.dao.isEmpty(this.request.getParameter(parameter));
	}

	/**
	 * @param request 要设置的 request
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = (HttpServletRequest)request;
	}
	
	public static void main(String[] args){
    	ComboboxUtils t = new ComboboxUtils();
System.out.println(t.list());
    }
	
}
